import 'dart:math';

import 'package:boss/color_constant.dart';
import 'package:boss/models/commodity.dart';
import 'package:boss/textStyle/text_style.dart';
import 'package:boss/ui/widget/image/base_image.dart';
import 'package:boss/ui/widget/number_editer.dart';
import 'package:boss/ui/widget/radioList/radio.dart' as radio;
import 'package:hi_common/hi_common.dart';
import 'package:common_utils/common_utils.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class CategoriesCommodityItem extends StatelessWidget {
  final Commodity data;
  final VoidCallback onTap;
  final int index;
  final bool isShowSku;
  final bool isHideIcon;
  final int type;
  final ValueChanged<int> onNumberChanged;

  const CategoriesCommodityItem({
    Key key,
    @required this.data,
    this.onTap,
    this.index,
    this.isShowSku = false,
    this.isHideIcon = false,
    this.type,
    this.onNumberChanged,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    Widget child = GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTap: onTap,
      child: Container(
        constraints: BoxConstraints(minHeight: 220.w),
        padding: EdgeInsets.symmetric(horizontal: 20.w),
        child: Padding(
          padding: EdgeInsets.symmetric(vertical: 30.w - 1.px / 2),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              Container(
                margin: EdgeInsets.only(right: 20.w),
                child: BaseImage(
                  data.images,
                  width: 160.w,
                  height: 160.w,
                  borderRadius: BorderRadius.circular(10),
                ),
              ),
              Expanded(
                child: Container(
                  constraints: BoxConstraints(minHeight: 160.w),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: <Widget>[
                      Text(
                        data.title + (data.commodityType == Commodity.COMMODITY_TYPE_PACKAGE ? '[套餐]' : ''),
                        style: TextStyle(
                          fontSize: 32.sp,
                          color: ColorConstant.ff33,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      if (isShowSku && data.sku != null) Text(data.sku.skuName, style: TextStyleConstant().normal_3_22),
                      _buildPriceText(),
                    ],
                  ),
                ),
              ),
              if (type == 0) radio.Radio(isSelected: data.isSelected),
              if (type == 1) NumberEditer(value: data.count, onChanged: onNumberChanged),
            ],
          ),
        ),
      ),
    );

    return child;
  }

  Widget _buildPriceText() {
    int price = 0;
    String end = '';

    if (isShowSku) {
      price = data.sku?.skuPrice ?? data.price;
    } else {
      if (data.commoditySkuList.length > 0) {
        price = data.commoditySkuList[0].skuPrice;

        data.commoditySkuList.forEach((item) {
          price = min(price, item.skuPrice);
        });
      } else {
        price = data.price;
      }
    }

    if (data.commoditySkuList.length > 0 && !isShowSku) {
      end = '起';
    }

    var priceStr = MoneyUtil.changeF2Y(price);

    var priceSplit = priceStr.split('.');
    var yuan = priceSplit[0];
    var jf = priceSplit[1];
    return RichText(
      text: TextSpan(
        text: '￥',
        style: TextStyle(fontSize: 20.sp, color: ColorConstant.fe7f76, fontWeight: FontWeight.bold),
        children: [
          TextSpan(
            text: yuan,
            style: TextStyle(fontSize: 36.sp, color: ColorConstant.fe7f76, fontFamily: 'DINAlternate-Bold', fontWeight: FontWeight.bold),
          ),
          TextSpan(
            text: '.$jf',
            style: TextStyle(fontSize: 26.sp, color: ColorConstant.fe7f76, fontFamily: 'DINAlternate-Bold', fontWeight: FontWeight.bold),
          ),
          TextSpan(
            text: '/${data.unit == '' ? '份' : data.unit}$end',
            style: TextStyle(fontSize: 20.sp, color: ColorConstant.fe7f76, fontFamily: 'DINAlternate-Bold', fontWeight: FontWeight.bold),
          ),
        ],
      ),
    );
  }
}
